﻿import random

# import paho.mqtt.client as mqtt
# import time
# 
# # MQTT 消息处理函数
# def on_message(client, userdata, message):
#     print(f"Received message: {message.payload.decode()}")
# 
# 
# # MQTT 连接回调函数
# def on_connect(client, userdata, flags, rc):
#     print("Connected with result code " + str(rc))
#     client.subscribe("car_fsm/state")  # 在连接成功后订阅主题
# 
# 
# def on_disconnect():
#     print("DisConnected with result code ")
# 
# 
# 
# # 创建 MQTT 客户端
# client = mqtt.Client()
# 
# client.on_connect = on_connect  # 设置连接回调函数
# client.on_message = on_message
# client.on_disconnect = on_disconnect
# 
# 
# # 连接 MQTT Broker 的重连机制
# while True:
#     try:
#         client.connect("localhost", 1883, 60)  # 尝试连接到本地的 MQTT Broker
#         break  # 连接成功后跳出循环
#     except ConnectionRefusedError:
#         print("MQTT Broker not available, retrying in 5 seconds...")
#         time.sleep(5)  # 等待 5 秒后重试
# 
# # 开始监听
# client.loop_forever()

from node_red_msg_reader import node_red_msg_reader
import  sys

topic = sys.argv[1]

def msg_process(msg_data):
    print(msg_data)

msg_tool = node_red_msg_reader(topic,msg_process)
msg_tool.connect()

